<template>
  <d2-container>

    <template slot="header">

      <el-form :gutter="24" ref="search-box" :model="mainQuery" label-position="left" :inline="true" label-width="90px" size="mini">

        <el-row>

          <el-col :span="8">
            <el-form-item :label="$t('page-form.form.url')">
              <el-input placeholder="" v-model="mainQuery.url" @keyup.enter.native="fetchMainData" clearable></el-input>
            </el-form-item>
          </el-col>

        </el-row>

      </el-form>

      <div class="search-btn-box">

        <div class="left">
          <el-button-group>
            <el-button size="mini" class="filter-item" icon="el-icon-plus" type="success" @click="handleAdd" :loading="mainTable.loading">{{$t('page-form.form.add')}}</el-button>
          </el-button-group>
        </div>
        <div class="right">
          <el-button size="mini" class="filter-item" icon="el-icon-refresh" type="default" @click="resetMainData" :loading="mainTable.loading">{{ $t('page.search-box.reset') }}</el-button>
          <el-button size="mini" class="filter-item" icon="el-icon-search" type="primary" @click="fetchMainData" :loading="mainTable.loading">{{ $t('page.search-box.search') }}</el-button>
        </div>
      </div>

    </template>

    <!--主列表数据-->
    <d2-crud ref="d2Crud" :selection-row="mainQuery.allowSelect" :options="mainTable.options" :index-row="mainTable.indexRow" :columns="mainTable.columns" :data="mainTable.list" :loading="mainTable.loading" :rowHandle="mainTable.rowHandle" @current-change="onMainCurrentChange" @selection-change="onMainSelectionChange" @handle-set="handleSet" @handle-del="handleDel"></d2-crud>

    <template slot="footer">
      <el-pagination @size-change="handleMainSizeChange" @current-change="handleMainPageChange" :current-page="mainTable.pagination.currentPage" :page-sizes="mainTable.pagination.pageSizes" :page-size="mainTable.pagination.pageSize" :page-count="mainTable.pagination.pageCount" :total="mainTable.pagination.total" layout="total, sizes, prev, pager, next, jumper" :background="true">
      </el-pagination>
    </template>

    <dialog-add ref="dialog-add" @dialog-close="fetchMainData">
    </dialog-add>

  </d2-container>
</template>

<script>

import ApiMain from '@/api/trade.website';
import util from '@/libs/util.js';
import { approveStatusOptions } from '@/const/constant';

import DialogAdd from './dialog-add';
import i18nTable from "@/mixins/i18n-table";

export default {
  name: 'trade-website',
  mixins: [i18nTable
  ],
  components: {
    DialogAdd,
  },
  data() {
    return {

      approveStatusOptions,

      mainQueryRaw: null,
      mainQuery: {
        terNo: '',
        url: '',

      },

      mainTable: {
        loading: false,
        options: {
          border: true,
          stripe: true,
          highlightCurrentRow: true,
          emptyText: this.$t('page-announcement.d2-crud.empty-data'),
          size: 'mini',
        },
        // indexRow: {
        //   title:this.$t('page.form.serialnumber'),
        //   width: "80",
        //   align:'center'
        // },
        indexRow: null,
        allowSelect: false,
        columns: [
          { title: "网站", t: "page-form.form.url", key: 'url', align: 'center', width: '180', showOverflowTooltip: true },
          { title: "网站备案号", t: "page-form.form.put-records", key: 'beianNo', align: 'center', width: '180', showOverflowTooltip: true },
          { title: "审批状态", t: "page-form.form.approval-status", key: 'strReviewStatus', align: 'center', width: '180', showOverflowTooltip: true },
          { title: "申请人", t: "page-form.form.proposer", key: 'createBy', align: 'center', width: '180', showOverflowTooltip: true },
          { title: "申请时间", t: "page-form.form.timeofapplication", key: 'createTime', align: 'center', width: '180', showOverflowTooltip: true },
          { title: "审批时间", t: "page-form.form.approvaltime", key: 'updateTime', align: 'center', width: '180', showOverflowTooltip: true },
        ],
        rowHandle: {
          width: '260',
          text: "操作",
          align: 'center',
          fixed: 'right',
           t: "page.form.operation",
          columnHeader: this.$t('page.form.operation'),
          custom: [
            {
              text: "修改",
              t: "page-form.form.modification",
              type: 'default',
              size: 'mini',
              emit: 'handle-set'
            },
            {
              text: "删除",
              t: "page-form.form.delete",
              type: 'danger',
              size: 'mini',
              plain: true,
              emit: 'handle-del'
            }
          ]
        },
        list: [],
        pagination: {
          currentPage: 1,
          pageSize: 50,
          total: 0,
          pageSizes: [50, 100, 200]
        },
        selectionRow: [],
      },
    }
  },
  mounted() {
    this.mainQueryRaw = Object.copy(this.mainQuery);

    this.fetchOptions().finally(() => {
      this.resetMainData();
    });
  },
  beforeDestroy() {
  },
  methods: {

    renderMainData(re) {
      this.mainTable.list = re.list;
      this.mainTable.pagination.total = re.total;
    },

    handleMainSizeChange(val) {
      this.mainTable.pagination.pageSize = val;
      this.fetchMainData()
    },
    handleMainPageChange(val) {
      this.mainTable.pagination.currentPage = val;
      this.fetchMainData()
    },

    buildMainQuery() {

      var query = {
        page: this.mainTable.pagination.currentPage,
        limit: this.mainTable.pagination.pageSize,
        search: this.mainQuery,
      };

      return query;
    },

    resetMainData() {

      this.mainQuery = Object.copy(this.mainQueryRaw);

      this.fetchMainData();
    },

    async fetchOptions() {

    },

    async fetchMainData() {

      this.mainTable.loading = true;

      try {
        var re = await ApiMain.list(this.buildMainQuery());
        this.renderMainData(re);
      } catch (err) { }

      this.mainTable.loading = false;

    },

    onMainCurrentChange(now, old) {
      this.mainTable.current = now;
    },

    onMainSelectionChange(now, old) {
      this.mainTable.selectionRow = now
    },

    onMainDoubleClick(now) {
      this.mainTable.current = now;
      this.handleSet();
    },

    handleAdd() {
      this.$refs['dialog-add'].show();
    },

    handleSet({ index, row }) {
      if (row.approveStatus != 1) {
        this.$message({ message: this.$t('page-form.form.tips1'), type: 'error' });
        return false;
      }

      this.$refs['dialog-add'].show(Object.copy(row));
    },

    handleDel({ index, row }) {

      this.$confirm(this.$t('page.form.Suretodelete'), this.$t('page.form.hint'), {
        confirmButtonText: this.$t('page.form.confirm'),
        cancelButtonText: this.$t('page.form.cancel'),
        type: 'warning'
      }).then(() => {

        var req = { id: row.id };

        ApiMain.del(req).then(() => {
          this.$message({ message: this.$t('page.form.operatesuccessfully'), type: 'success' });
          this.fetchMainData();
        });

      });
    },

  }
}
</script>

<style>
</style>
